package sdmakerz_v1.coresyntax.generator

import sdmakerz_v1.coresyntax.generator.interfaces.IConfigData
import sdmakerz_v1.coresyntax.sDMakerV1.Config

/**
 * Une classe qui simplfie la recuperation des donnees d'une Config
 * @author pstl-team
 */
class ConfigDataImpl implements IConfigData {
	
//	public static IConfigData EMPTY_CONFIG= computeEmptyConfig();
	private boolean isEmptyConfig;
	
	private boolean withEsperanceLongueurTracesUniformes;
	private boolean withMemUsed;
	private boolean withNbObservations;
	private boolean withTailleFSA;
	
	private boolean withPrecision;
	private int precisionNb;
	private boolean precisionTracesCompletes;
	
	private boolean withRecall;
	private int recallNb;
	private boolean recallUseUniformTraces;
	private boolean recallTracesCompletes;
	
	private boolean withTailleTraces;
	private int tailleTracesNbUniformes;
	
	private String configName;
	private double globalProbInAlt;
	private double probContinueLoop;
	private double probExecOpt;
	
	
	
	/* ****************** *
	 * ** INITIALIZERS ** *
	 * ****************** */
	
	override void init(Config config){	
		
		withEsperanceLongueurTracesUniformes= false;
		withMemUsed= false;
		withNbObservations= false;
		withTailleFSA= false;
	
		withPrecision= false;
		precisionNb= -1;
		precisionTracesCompletes= false;
	
		withRecall= false;
		recallNb= -1;
		recallUseUniformTraces= false;
		recallTracesCompletes= false;
	
		withTailleTraces= false;
		tailleTracesNbUniformes= -1;
		
		globalProbInAlt= -1;
		probContinueLoop= -1;
		probExecOpt= -1;
		
		isEmptyConfig= (config==null);
		
		if(isEmptyConfig==false){

			configName= config.name;
		
			if(config.globalProbInAlt != null &&
			    0 < config.globalProbInAlt.value && 
			        config.globalProbInAlt.value <= 1){
				globalProbInAlt= config.globalProbInAlt.value;	
			}
			if(config.probContinueLoop != null &&
			    0 < config.probContinueLoop.value && 
			        config.probContinueLoop.value <= 1){
				probContinueLoop= config.probContinueLoop.value;
			}
			if(config.probExecOpt != null &&
			    0 < config.probExecOpt.value && 
			        config.probExecOpt.value <= 1){
				probExecOpt= config.probExecOpt.value;
			}
		
			for(String cmd : config.cmds){
				if(cmd.compareTo("esperanceLongueurTracesUniformes")==0){
					withEsperanceLongueurTracesUniformes= true;
				}else if(cmd.compareTo("memUsed")==0){
					withMemUsed= true;
				}else if(cmd.compareTo("nbObservations")==0){
					withNbObservations= true;
				}else if(cmd.compareTo("precision")==0){
					withPrecision= true;
					precisionNb= config.precisionNb.value;
					precisionTracesCompletes= 
					   (config.precisionTracesCompletes!=null);
				}else if(cmd.compareTo("recall")==0){
					withRecall= true;
					recallNb= config.recallNb;
					recallUseUniformTraces= 
					   (config.recallUseUniformTraces!=null);
					recallTracesCompletes= 
					   (config.recallTracesCompletes!=null);
				}else if(cmd.compareTo("tailleFSA")==0){
					withTailleFSA= true;
				}else if(cmd.compareTo("tailleTraces")==0){
					withTailleTraces= true;
					tailleTracesNbUniformes= 
					   config.tailleTracesNbUniformes.value;
				}
			}
		}
	}
	
	
	
    /* ************* *
	 * ** GETTERS ** *
	 * ************* */
	
	override double globalProbInAlt(){
		return globalProbInAlt;
	}
	
	override double probContinueLoop(){
		return probContinueLoop;
	}
	
	override double probExecOpt(){
		return probExecOpt;
	}
	
	def String getConfigName(){
		return configName;
	}	
	
	
	
	/* ************ *
	 * ** OTHERS ** *
	 * ************ */
	
	override CharSequence printImports(){
		var CharSequence charSequenceResult='''''';
		
		if(isEmptyConfig==false){
			charSequenceResult= charSequenceResult +
			'''import evaluation.*;'''
		}
		
		return charSequenceResult;
	}
	
	override CharSequence printEval(){
		var CharSequence charSequenceResult=''''''
		
		if(isEmptyConfig==false){
			charSequenceResult='''
		/* from config «configName» */
		Evaluator evaluator= new Evaluator(
		new Evaluator_Config() {

			@Override
			public void config() {
				«IF withEsperanceLongueurTracesUniformes»
				new EsperanceLongueurTracesUniformes();
				«ENDIF»
				«IF withMemUsed»
				new MemUsed();
				«ENDIF»
				«IF withNbObservations»
				new NbObservations();
				«ENDIF»
				«IF withPrecision»
				new Precision(«precisionNb», «precisionTracesCompletes»);
				«ENDIF»
				«IF withRecall»
				new Recall(«recallUseUniformTraces», «recallNb», «recallTracesCompletes»);
				«ENDIF»
				«IF withTailleFSA»
				new TailleFSA();
				«ENDIF»
				«IF withTailleTraces»
				new TailleTrace(«tailleTracesNbUniformes»);
				«ENDIF»
			}
		});
		
		System.out.println("Begin evaluation with «configName»: ");
		evaluator.eval("");
		System.out.println("End evaluation with «configName»");
		'''
		}
		
		return charSequenceResult;
	}
}